<template>
  <view class="lovetest">
    <view class="her">为了测试结果更为精准，以下内容请全部必填</view>
    <view class="lovetestList">
      <form class="myinfo" @submit="submitForm">
        <!-- 题目1 -->
        <view v-for="(item,index) in lovetestList" :key="item.id" class="lovetestItem">
          <view class="Title">{{ index + 1 }}．{{ item.title }}</view>
          <!--          <radio-group @change="(e) => handleChange(e, item.id)">-->
          <radio-group @change="(e) => handleChange(e, item.id,item.jsonstr)">
            <label v-for="(jsonstrItem, index) in item.jsonstr" :key="index" class="uni-list-cell uni-list-cell-pd">
              <view class="styRadio">
                <radio :value="jsonstrItem.i"/>
                <text class="txt">
                  {{ jsonstrItem.v }}
                </text>
              </view>
            </label>
          </radio-group>
        </view>
        <view class="blank"></view>
        <!-- 提交按钮 -->
        <view class="bntFlex">
          <button class="nextbtn" form-type="submit">提交看结果</button>
        </view>
      </form>
    </view>
  </view>
</template>

<script>
var app = getApp();

export default {
  data() {
    return {
      lovetestList: [],
      // 所有题目的答案存储对象
      answers: {},
      id: '',
    }
  },
  onLoad(options) {
    if (options.id) {
      this.id = options.id;
      this.init()
    }
  },


  methods: {
    init() {
      const that = this;
      const a = uni.getStorageSync('COOK_U')
      var o = {
        submitok: 'subject',
        openid: a.openid,
        id: this.id,
      };
      uni.showLoading({
        title: '玩命加载中',
        mask: true
      });
      app.globalData.ajax({
        url: 'emotion.php',
        data: o,
        success: function (e) {
          uni.hideLoading();
          if ('1' === e.flag) {
            if (e.answer === '1') {
              uni.redirectTo({
                url: '/pagesB/testResults/testResults?id=' + that.id,
              })
            } else {
              that.setData({
                lovetestList: e.question,
              });
            }

          } else {
            uni.showToast({
              title: e.msg,
              icon: 'none',
              mask: true
            });
          }
        }
      });
    },
    // 处理选项变化
    handleChange(e, id, jsonstr) {
      const option = e.detail.value
      const question = 'question' + id
      const fraction = this.findSValueByI(jsonstr, option)
      console.log('findSValueByI', this.findSValueByI(jsonstr, option));
      console.log('fraction', fraction);

      // 将答案存储到对应的题目ID下
      this.answers[question] = `${option}:${fraction}`;
      console.log(`题目 ${question} 选择了:`, this.answers[question]);
    },
    findSValueByI(arr, targetI) {
      // 使用find方法查找匹配的对象
      const foundItem = arr.find(item => item.i === targetI);

      // 如果找到对象，返回其s值；否则返回null
      return foundItem ? foundItem.s : null;
    },
    // 提交表单
    submitForm() {
      const that = this;
      const a = uni.getStorageSync('COOK_U')
      for (const item of this.lovetestList) {
        const answerKey = 'question' + item.id
        if (!(answerKey in this.answers)) {
          uni.showToast({
            title: '请填写完测试题目在提交',
            icon: 'none'
          });
          return;
        }
      }
      console.log('所有答案:', this.answers);
      var o = {

        data: this.answers,
      };
      uni.showLoading({
        title: '玩命加载中',
        mask: true
      });
      app.globalData.ajax({
        url: 'emotion.php',
        data: {
          submitok: 'ajax_save',
          openid: a.openid,
          id: that.id,
          ...this.answers,
        },
        success: function (e) {
          console.log('提交返回信息', e)
          uni.hideLoading();
          if ('1' === e.flag) {
            // 提交逻辑...
            uni.showToast({
              title: e.msg,
              icon: 'none',
              duration: 2000,
            });
            setTimeout(function () {
              uni.navigateTo({
                url: '/pagesB/testResults/testResults?id=' + that.id,
              })
            }, 2000)
          } else {
            uni.showToast({
              title: e.msg,
              icon: 'none',
              mask: true
            });
          }
        }
      });

    }
  }
}
</script>

<style lang="scss" scoped>
@import "lovetest.scss";
</style>